package com.tul.vapp.servicecenter.controller;

import com.tul.vapp.servicecenter.bean.ResponseBean;
import com.tul.vapp.servicecenter.common.ErrorCodeConstant;
import com.tul.vapp.servicecenter.util.MessageUtil;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

import com.tul.cusa.common.exception.BizException;
import com.tul.cusa.common.exception.ExceptionConstant;
import com.tul.cusa.common.exception.SysException;
import com.tul.cusa.common.log.LogTracerFactory;
import com.tul.cusa.common.log.intf.ILogTracer;
import com.tul.cusa.common.util.StringUtil;



/**
 * BaseController
 * Created by wangziang on 2016/5/30.
 */
@RestController
public class ExceptionController {
    /**
	 * 日志
	 */
	private static final ILogTracer TRACER = LogTracerFactory.getTracer(ExceptionController.class);

    /**
     * handleDataAccessException
     *
     * @param e e
     * @return responseBean
     */
    @ExceptionHandler({DataAccessException.class})
    public ResponseBean handleDataAccessException(DataAccessException e) {
        ResponseBean responseBean = new ResponseBean();
        responseBean.setSuccess(false);
		responseBean.setErrorLevel(ExceptionConstant.LEVEL_SYS);
		responseBean.setErrorCode(ErrorCodeConstant.SYS_DB_ACCESS_ERROR);
		responseBean.setErrorMsg(MessageUtil.getMessage(ErrorCodeConstant.SYS_DB_ACCESS_ERROR));
		TRACER.error("数据库操作异常", e);
        return responseBean;
    }

    /**
     * handleException
     *
     * @param e e
     * @return responseBean
     */
    @ExceptionHandler({OptimisticLockingFailureException.class})
    public ResponseBean handleOptimisticLockException(OptimisticLockingFailureException e) {
        ResponseBean responseBean = new ResponseBean();
        responseBean.setSuccess(false);
		responseBean.setErrorLevel(ExceptionConstant.LEVEL_SYS);
		responseBean.setErrorCode(ErrorCodeConstant.SYS_DB_MODIFIED_DATA_EXPIRIED);
		responseBean.setErrorMsg(MessageUtil.getMessage(ErrorCodeConstant.SYS_DB_MODIFIED_DATA_EXPIRIED));
		TRACER.error("乐观锁异常", e);
        return responseBean;
    }


    /**
     * handleException
     *
     * @param e e
     * @return responseBean
     */
	@ExceptionHandler({ BizException.class })
	public ResponseBean handleBizException(BizException e) {
		ResponseBean responseBean = new ResponseBean();
		responseBean.setErrorLevel(ExceptionConstant.LEVEL_BIZ);
		responseBean.setSuccess(false);
		String errorCode = StringUtil.replaceNull(e.getErrorCode(), ErrorCodeConstant.BIZ_UNKNOWN);
		String message = MessageUtil.getExceptionMsg(e);
		responseBean.setErrorCode(errorCode);
		responseBean.setErrorMsg(message);
		TRACER.error(message, e);
		return responseBean;
	}

	/**
	 * handleException
	 *
	 * @param e e
	 * @return responseBean
	 */
	@ExceptionHandler({ SysException.class })
	public ResponseBean handleSysException(SysException e) {
		ResponseBean responseBean = new ResponseBean();
		responseBean.setErrorLevel(ExceptionConstant.LEVEL_SYS);
		responseBean.setSuccess(false);
		String errorCode = StringUtil.replaceNull(e.getErrorCode(), ErrorCodeConstant.SYS_UNKNOWN);
		String message = MessageUtil.getExceptionMsg(e);
		responseBean.setErrorCode(errorCode);
		responseBean.setErrorMsg(message);
		TRACER.error(message, e);
		return responseBean;
	}

	/**
	 * handleException
	 *
	 * @param e e
	 * @return responseBean
	 */
	@ExceptionHandler({ Exception.class })
	public ResponseBean handleException(Exception e) {
		ResponseBean responseBean = new ResponseBean();
		responseBean.setErrorLevel(ExceptionConstant.LEVEL_SYS);
		responseBean.setSuccess(false);
		String errorCode = ErrorCodeConstant.SYS_UNKNOWN;
		responseBean.setErrorCode(errorCode);
		String message = MessageUtil.getExceptionMsg(e);
		responseBean.setErrorMsg(message);
		TRACER.error(message, e);
		return responseBean;
	}

	/**
	 * UnauthenticatedException
	 *
	 * @param e exception
	 * @return responseBean
	 */
	@ExceptionHandler({ UnauthenticatedException.class })
	public ResponseBean handleUnauthenticatedException(UnauthenticatedException e) {
		ResponseBean responseBean = new ResponseBean();
		responseBean.setSuccess(false);
		responseBean.setErrorLevel(ExceptionConstant.LEVEL_SYS);
		responseBean.setErrorCode(ErrorCodeConstant.SYS_USER_NOT_LOGIN_OR_SESSION_TIMEOUT);
		responseBean
				.setErrorMsg(MessageUtil.getMessage(ErrorCodeConstant.SYS_USER_NOT_LOGIN_OR_SESSION_TIMEOUT));
		return responseBean;
	}

	/**
	 * UnauthorizedException
	 *
	 * @param e exception
	 * @return responseBean
	 */
	@ExceptionHandler({ UnauthorizedException.class })
	public ResponseBean handleUnauthorizedException(UnauthorizedException e) {
		ResponseBean responseBean = new ResponseBean();
		responseBean.setSuccess(false);
		responseBean.setErrorLevel(ExceptionConstant.LEVEL_SYS);
		responseBean.setErrorCode(ErrorCodeConstant.SYS_USER_UN_AUTHORIZED);
		responseBean.setErrorMsg(MessageUtil.getMessage(ErrorCodeConstant.SYS_USER_UN_AUTHORIZED));
		return responseBean;
	}
}